unit Unit24; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, sqldb, db,
  Menus, DBGrids, Unit1, Unit29, Unit30, Unit31, Unit32, Unit33, Unit34,
  LR_DBSet, LR_Class, Unit55, Unit56, Unit57;

type

  { TForm24 }

  TForm24 = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    frDBDataSet1: TfrDBDataSet;
    frReport1: TfrReport;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem11: TMenuItem;
    MenuItem12: TMenuItem;
    MenuItem13: TMenuItem;
    MenuItem14: TMenuItem;
    MenuItem15: TMenuItem;
    MenuItem16: TMenuItem;
    MenuItem17: TMenuItem;
    MenuItem18: TMenuItem;
    MenuItem19: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    SQLQuery1: TSQLQuery;
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure MenuItem11Click(Sender: TObject);
    procedure MenuItem15Click(Sender: TObject);
    procedure MenuItem16Click(Sender: TObject);
    procedure MenuItem17Click(Sender: TObject);
    procedure MenuItem18Click(Sender: TObject);
    procedure MenuItem19Click(Sender: TObject);
    procedure ordenar_grid();
    procedure FormShow;
    procedure MenuItem10Click(Sender: TObject);
    procedure MenuItem12Click(Sender: TObject);
    procedure MenuItem13Click(Sender: TObject);
    procedure MenuItem14Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem6Click(Sender: TObject);
    procedure MenuItem8Click(Sender: TObject);
    procedure MenuItem9Click(Sender: TObject);
  private
    familia_producto : string;
    producto : String;
    nombre_sucursal : string;
    sobrante_faltante : string;
  public
    { public declarations }
  end; 

var
  Form24: TForm24; 

implementation

{ TForm24 }

procedure TForm24.ordenar_grid();
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  SQLQuery1.SQL.Text := 'select * from vw_stock where fmp_nombre like ''' + familia_producto
    + ''' and prd_nombre ilike ''%' + producto + '%'' and suc_nombre ilike ''%'
    + nombre_sucursal + '%''' + sobrante_faltante + ' order by prd_nombre';


  SQLQuery1.Open;

  DBGrid1.Columns[0].Title.Caption := 'Código';
  DBGrid1.Columns[1].Title.Caption := 'Nombre';
  DBGrid1.Columns[2].Title.Caption := 'Descripción';
  DBGrid1.Columns[3].Title.Caption := 'Familia de Productos';
  DBGrid1.Columns[4].Title.Caption := 'Sucursal';
  DBGrid1.Columns[5].Title.Caption := 'Stk. Min.';
  DBGrid1.Columns[6].Title.Caption := 'Stk. Max.';
  DBGrid1.Columns[7].Title.Caption := 'Stk. Actual';
  
  DBGrid1.Columns[0].Width := 90;
  DBGrid1.Columns[1].Width := 150;
  DBGrid1.Columns[2].Width := 150;
  DBGrid1.Columns[3].Width := 150;
  DBGrid1.Columns[4].Width := 100;
  DBGrid1.Columns[5].Width := 60;
  DBGrid1.Columns[6].Width := 60;
  DBGrid1.Columns[7].Width := 70;

  TColumn(DBGrid1.Columns[3]).DisplayFormat := '$ ####0.00';
end;

procedure TForm24.FormShow;
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
    
  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;

  familia_producto := '%';
  producto := '%';
  nombre_sucursal := Form1.mi_sucursal;
  sobrante_faltante := '';
  
  ordenar_grid;
end;

procedure TForm24.MenuItem10Click(Sender: TObject);
begin
  Form34.ShowModal;
end;

procedure TForm24.MenuItem12Click(Sender: TObject);
begin
  Form33.ShowModal;
  
  if familia_producto <> '' then
  begin
    familia_producto := Form33.familia_producto;
    ordenar_grid;
  end;
end;

procedure TForm24.MenuItem13Click(Sender: TObject);
begin
  Form31.ShowModal;
  FormShow;
end;

procedure TForm24.MenuItem14Click(Sender: TObject);
begin
  Form32.ShowModal;
  FormShow;
end;

procedure TForm24.MenuItem2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm24.MenuItem5Click(Sender: TObject);
begin
  Form29.ShowModal;
  FormShow;
end;

procedure TForm24.MenuItem6Click(Sender: TObject);
begin
  Form30.ShowModal;
  FormShow;
end;

procedure TForm24.MenuItem8Click(Sender: TObject);
begin
  producto := InputBox('Filtrar por Producto', 'Ingrese parte del nombre del producto:', '');
  
  if producto <> '' then
    ordenar_grid
  else
    producto := '%';
end;

procedure TForm24.MenuItem9Click(Sender: TObject);
begin
  FormShow;
end;

procedure TForm24.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
end;

procedure TForm24.MenuItem11Click(Sender: TObject);
begin
  frReport1.LoadFromFile('reporte_stock.lrf');
  frReport1.ShowReport;
end;

procedure TForm24.MenuItem15Click(Sender: TObject);
begin
  Form55.ShowModal;
  
  nombre_sucursal := Form55.nombre_sucursal;
  
  ordenar_grid;
end;

procedure TForm24.MenuItem16Click(Sender: TObject);
begin
  Form56.ShowModal;
  
  ordenar_grid();
end;

procedure TForm24.MenuItem17Click(Sender: TObject);
begin
  Form57.ShowModal;
  
  ordenar_grid;
end;

procedure TForm24.MenuItem18Click(Sender: TObject);
begin
  sobrante_faltante := ' and vw_stock.stk_cantidad <= (select prd_stock_minimo from producto where '
    + 'producto.prd_cdg = vw_stock.prd_cdg) ';
    
  ordenar_grid();
end;

procedure TForm24.MenuItem19Click(Sender: TObject);
begin
    sobrante_faltante := ' and vw_stock.stk_cantidad >= (select prd_stock_maximo from producto where '
    + 'producto.prd_cdg = vw_stock.prd_cdg) ';

  ordenar_grid();
end;

initialization
  {$I unit24.lrs}

end.

